home *** CD-ROM | disk | FTP | other *** search
- /* implementation file for the stack allocation package. */
-
- /* get definition of size_t */
- #include <stdlib.h>
-
- #include "align.h"
- #include "stckallc.h"
-
- /* target size for each allocation block */
- #define TARGET_ALLOC 128
-
- #define N_UNITS_IN_POINTER N_ALIGN_UNITS(sizeof(void *))
-
- /* initialize allocation stack */
- void init_alloc_stack
- (
- /* pointer to allocation stack to initialize */
- ALLOC_STACK *as,
- /* sizeof elements to be allocated */
- size_t elem_size
- )
- {
- as->n_units_in_element = N_ALIGN_UNITS(elem_size);
-
- as->n_units_in_block =
- (N_ALIGN_UNITS(TARGET_ALLOC) - N_UNITS_IN_POINTER +
- as->n_units_in_element - 1) / as->n_units_in_element;
- as->n_units_in_block *= as->n_units_in_element;
- as->n_units_in_block += N_UNITS_IN_POINTER;
-
- /* set free index so that a new block will be allocated on the
- 1st call to get_alloc_stack() */
- as->free = as->n_units_in_block;
-
- as->block_list = (void *) 0;
-
- return;
- }
-
- /* allocate an element from the allocation stack. returns null if
- insufficient memory available */
- void *get_alloc_stack
- (
- /* pointer to allocation stack */
- ALLOC_STACK *as
- )
- {
- void *result;
-
- if (as->free == as->n_units_in_block)
- {
- /* get another block from the heap */
- result = malloc(as->n_units_in_block * sizeof(ALIGN_TYPE));
- if (!result)
- return((void *) 0);
- /* insert the block in the list of blocks at the beginning */
- *((void **) result) = as->block_list;
- as->block_list = result;
-
- as->free = N_UNITS_IN_POINTER;
- }
-
- result = ((ALIGN_TYPE *) as->block_list) + as->free;
- as->free += as->n_units_in_element;
-
- return(result);
- }
-
- /* return address of last element allocated that was not later freed */
- void *look_alloc_stack
- (
- /* pointer to allocation stack */
- const ALLOC_STACK *as
- )
- {
- return(((ALIGN_TYPE *) as->block_list) +
- (as->free - as->n_units_in_element));
- }
-
- /* free last element allocated */
- void free_alloc_stack
- (
- /* pointer to allocation stack */
- ALLOC_STACK *as
- )
- {
- as->free -= as->n_units_in_element;
- if (as->free == N_UNITS_IN_POINTER)
- {
- void *p = as->block_list;
- as->block_list = *((void **) p);
- free(p);
- as->free = as->n_units_in_block;
- }
-
- return;
- }
-
- /* free all elements allocated */
- void clear_alloc_stack
- (
- /* pointer to allocation stack */
- ALLOC_STACK *as
- )
- {
- void *p;
-
- while (as->block_list)
- {
- p = as->block_list;
- as->block_list = *((void **) p);
- free(p);
- }
-
- /* set free index so that a new block will be allocated on the
- 1st call to get_alloc_stack() */
- as->free = as->n_units_in_block;
-
- return;
- }
-